const util = require('../../../utils/util.js')
var xrRequest = require("../../../vendor/hope-sdk/core/request");
var config = require('../../../config');
const WxParse = require('../../../vendor/wxParse/wxParse.js');
const app = getApp()
Page({
  data: {
    id: 0,
    info: {},
    height: 64, //header高度
    top: 0, //标题图标距离顶部距离
    scrollH: 0, //滚动总高度
    opcity: 0,
    iconOpcity: 0.5,
    bannerIndex: 0,
    menuShow: false,
    popupShow: false,
    value: 1,
    collected: false,
    modalShow: false
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      id: options.id
    });
    this.initInfo();
    let obj = wx.getMenuButtonBoundingClientRect();
    this.setData({
      width: obj.left,
      height: obj.top + obj.height + 8,
      top: obj.top + (obj.height - 32) / 2
    }, () => {
      wx.getSystemInfo({
        success: (res) => {
          this.setData({
            scrollH: res.windowWidth
          })
        }
      })
    });
  },
  initInfo: function () {
    var that = this;
    xrRequest.request(app,{
      url: config.service.goodsInfo,
      login: false,
      data: {
        id: that.data.id
      },
      success: function (res) {
        that.setData({
          info: res.data.data
        });
        if (!util.isEmpty(res.data.data.goods_content))
          WxParse.wxParse('articlecontent', 'html', res.data.data.goods_content, that, 5);
      }
    });
  },
  bannerChange: function (e) {
    this.setData({
      bannerIndex: e.detail.current
    })
  },
  previewImage: function (e) {
    let index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.info.images[index],
      urls: this.data.info.images
    })
  },
  //页面滚动执行方式
  onPageScroll(e) {
    let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop;
    let opcity = scroll / this.data.scrollH;
    if (this.data.opcity >= 1 && opcity >= 1) {
      return;
    }
    this.setData({
      opcity: opcity,
      iconOpcity: 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
    })
  },
  back: function () {
    wx.navigateBack()
  },
  openMenu: function () {
    this.setData({
      menuShow: true
    })
  },
  closeMenu: function () {
    this.setData({
      menuShow: false
    })
  },
  showPopup: function () {
    this.setData({
      popupShow: true
    })
  },
  hidePopup: function () {
    this.setData({
      popupShow: false
    })
  },
  change: function (e) {
    this.setData({
      value: e.detail.value
    })
  },
  collecting: function () {
    this.setData({
      collected: !this.data.collected
    })
  },
  common: function () {
    util.toast("功能开发中~")
  },
  submit() {
    if (app.globalData.logined && app.globalData.userinfo.is_anonymous == 1) {
      wx.navigateTo({
        url: '../../login/login'
      })
    } else {
      this.showModal();
    }

  },
  coupon() {
    wx.navigateTo({
      url: '../mall-extend/coupon/coupon'
    })
  },
  showModal() {
    this.setData({
      modalShow: true
    })
  },
  hideModal() {
    this.setData({
      modalShow: false
    })
  },
  handleClick: function (e) {
    var that = this;
    let index = e.detail.index;
    if (index == 1) {
      //util.toast("你点击的按钮index：" + index)
      that.enrollHandler();
    }
    this.hideModal()
  },
  // 报名处理
  enrollHandler: function () {
    var that = this;
    xrRequest.request(app,{
      url: config.service.addOrder,
      login: true,
      auth: true,
      data: {
        job_id: that.data.id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
          wx.redirectTo({
            url: '../success/success'
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { },
    })
  },
  go:function(e){
    let page = Number(e.currentTarget.dataset.page);
    switch (page) {
      case 1:
        wx.switchTab({
          url: '../../index/index',
        })
        break;
      case 2:
        wx.makePhoneCall({
          phoneNumber: '4000339066' //仅为示例，并非真实的电话号码
        })
        break;
      default:
        break;
    }
  }
})